package com.demo4;

import java.util.Arrays;
import java.util.Comparator;
import java.util.Random;
import java.util.Scanner;

/**
 * <pre>
 * -
 * Author : J.L.Zhou
 * E-Mail : 2233875735@qq.com
 * Tel : 151 1104 7708
 * Date : 2021-04-09 9:58
 * Version : 1.0
 * Copyright 2021 jlzhou.top Inc. All rights reserved.
 * Warning: this content is only for internal circulation of the company.
 *          It is forbidden to divulge it or use it for other commercial purposes.
 * </pre>
 **/
public class MainShape {

  private static IShape[] shapes = new IShape[0];

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    sc.nextInt();
    while(true) {
      printMenu();
      int menu = sc.nextInt();
      IShape shape = null;
      switch (menu) {
        case 0:
          System.out.println("程序退出");
          return;
        case IShape.RECTANGLE:
          shape = new Rectangle();
          break;
        case IShape.ELLIPSE:
          shape = new Ellipse();

          break;
        case IShape.ROUNDNESS:
          shape = new Roundness();

          break;
        case IShape.SQUARE:
          shape = new Square();

          break;
        case IShape.TRILATERAL:
          shape = new Trilateral();
          break;

        case 6:
          printAll();
          break;
        case 7:
          order7();
          break;
        case 8:
          order8();
          break;
        case 9:
          order9();
          break;
        default:
          System.out.println("您输入的菜单错误!");
          break;
      }
      if(shape!=null) {
        shape.input(sc);
        shape.show();
        add(shape);
      }
    }
  }




  public static void printMenu(){
    System.out.println("---------菜单----------");
    System.out.println(" 1. 长方形");
    System.out.println(" 2. 三角形");
    System.out.println(" 3. 椭圆形");
    System.out.println(" 4. 圆形");
    System.out.println(" 5. 正方形");
    System.out.println(" 6. 显示输入的所有图形");
    System.out.println(" 7. 随机输出所有图形");
    System.out.println(" 8. 按面积从小到大排序输出图形");
    System.out.println(" 9. 按周长从大到小排序输出图形");
    System.out.println(" 10. 删除图形");
    System.out.println(" 0. 退出");
    System.out.println("----------------------");
    System.out.println("请输入菜单编号：");
  }

  /**
   * 打印所有输入的图形
   */
  public static void printAll(){
    if(shapes==null || shapes.length==0){
      System.out.println("没有输入图形!");
      return ;
    }
    for(IShape shape : shapes){
      shape.show();
    }
  }

  /**
   * 将图形加入数组中
   * @param shape
   */
  public static void add(IShape shape){
    IShape[] newArrays = new IShape[shapes.length+1];
    System.arraycopy(shapes,0,newArrays,0,shapes.length);
    newArrays[shapes.length]=shape;
    shapes = newArrays;
  }

  public static void order7(){
//    Arrays.sort(shapes, new Comparator<IShape>() {
//      @Override
//      public int compare(IShape o1, IShape o2) {
//        return 0;
//      }
//    });
    Random r = new Random();
    Arrays.sort(shapes,(a,b)->r.nextInt());
    printAll();
  }

  public static void order8(){
    Arrays.sort(shapes,(a,b)->(int)(a.getArea()-b.getArea()));
    printAll();
  }

  public static void order9(){
    Arrays.sort(shapes,(a,b)->(int)(b.getPerimeter()-a.getPerimeter()));
    printAll();
  }
}
